package com.antexpress.dao;

import com.antexpress.pojo.Department;
import com.antexpress.pojo.Title;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Andy
 */
@Repository
public class DepartmentDAOImpl implements DepartmentDAO{
    @Autowired  
    private SessionFactory sessionfactory;
    
    private Session getSession(){  
        return sessionfactory.getCurrentSession();
    }  
    
    @Override
    public List<Department> getAllDept() {
        Session session = getSession();
        Criteria crit = session.createCriteria(Department.class);
        crit.addOrder(Order.desc("createtime"));
        List<Department> deptList = crit.list();
        return deptList;
    }

    @Override
    public List<Title> getTitleListByDept(String deptId) {
        Session session = getSession();
        Criteria crit = session.createCriteria(Title.class);
        Department dept = new Department();
        dept.setId(deptId);
        crit.add(Restrictions.eq("department", dept));
        crit.addOrder(Order.desc("authority"));
        List<Title> titleList = crit.list();
        return titleList;
    }

    @Override
    public boolean saveOrUpdateDepartment(Department dept) {
        Session session = getSession();
        boolean isSuccess = false;
        Transaction trans = null;
        try{
            trans = session.beginTransaction();
            session.saveOrUpdate(dept);
            trans.commit();
            session.flush();
            isSuccess = true;
        }catch(Exception e){
            e.printStackTrace();
            if(trans!=null)
                trans.rollback();
            isSuccess = false;
        }
        return isSuccess;
    }

    @Override
    public Department getDeptById(String id) {
        Session session = getSession();
        Criteria crit = session.createCriteria(Department.class);
        crit.add(Restrictions.eq("id", id));
        List<Department> deptList = crit.list();
        return deptList.get(0);
    }

    @Override
    public boolean saveOrUpdateTitle(Title title) {
        Session session = getSession();
        boolean isSuccess = false;
        Transaction trans = null;
        try{
            trans = session.beginTransaction();
            session.saveOrUpdate(title);
            trans.commit();
            session.flush();
            isSuccess = true;
        }catch(Exception e){
            e.printStackTrace();
            if(trans!=null)
                trans.rollback();
            isSuccess = false;
        }
        return isSuccess;
    }

    @Override
    public Title getTitleById(String id) {
        Session session = getSession();
        Criteria crit = session.createCriteria(Title.class);
        crit.add(Restrictions.eq("id", id));
        List<Title> titleList = crit.list();
        if(titleList.isEmpty())
            return null;
        return titleList.get(0);
    }

    @Override
    public List<Title> getAllTitle() {
        Session session = getSession();
        Criteria crit = session.createCriteria(Title.class);
        crit.addOrder(Order.desc("updatetime"));
        List<Title> titleList = crit.list();
        return titleList;
    }

    @Override
    public boolean deleteTitle(Title title) {
        Session session = getSession();
        boolean isSuccess = false;
        Transaction trans = null;
        try{
            trans = session.beginTransaction();
            session.delete(title);
            trans.commit();
            session.flush();
            isSuccess = true;
        }catch(Exception e){
            e.printStackTrace();
            if(trans!=null)
                trans.rollback();
            isSuccess = false;
        }
        return isSuccess;
    }

    @Override
    public boolean deleteDept(Department dept) {
        Session session = getSession();
        boolean isSuccess = false;
        Transaction trans = null;
        try{
            trans = session.beginTransaction();
            session.delete(dept);
            trans.commit();
            session.flush();
            isSuccess = true;
        }catch(Exception e){
            e.printStackTrace();
            if(trans!=null)
                trans.rollback();
            isSuccess = false;
        }
        return isSuccess;
    }

    @Override
    public boolean batchDeleteTitle(List<Title> titles) {
        Session session = getSession();
        boolean isSuccess = false;
        Transaction trans = null;
        try{
            trans = session.beginTransaction();
            for(Title t:titles)
                session.delete(t);
            trans.commit();
            session.flush();
            isSuccess = true;
        }catch(Exception e){
            e.printStackTrace();
            if(trans!=null)
                trans.rollback();
            isSuccess = false;
        }
        return isSuccess;
    }
    
    @Override
    public Title getTitleByName(String name){
        Session session = getSession();
        Criteria crit = session.createCriteria(Title.class);
        crit.add(Restrictions.eq("name", name));
        Title title = (Title) crit.uniqueResult();
        return title;
    }
    
}
